package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-06-27 10:14
 */
@Configuration
public class DeadLetterMqConfig {

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    /**
     * 注册交换机(交换机1,2)
     * @return
     */
    @Bean
    public DirectExchange exchange(){
        //autoDelete参数三: 当交换机没有绑定队列是否自动删除交换机
        return new DirectExchange(exchange_dead, true, false);
    }

    /**
     * 声明死信队列,消息超时发送到按照约定路由键到交换机
     * @return
     */
    @Bean
    public Queue queue1(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-dead-letter-exchange", exchange_dead);
        args.put("x-dead-letter-routing-key", routing_dead_2);
        args.put("x-message-ttl", 10000);
        //参数三:排他队列 只对首次声明它的连接（Connection）可见
        return new Queue(queue_dead_1, true, false, false, args);
    }

    @Bean
    public Binding binding() {
        // 将队列一 通过routing_dead_1 key 绑定到exchange_dead 交换机上
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
    }


    @Bean
    public Queue queue2(){
        //参数三:排他队列 只对首次声明它的连接（Connection）可见
        return new Queue(queue_dead_2, true, false, false);
    }

    @Bean
    public Binding binding2() {
        // 将队列一 通过routing_dead_1 key 绑定到exchange_dead 交换机上
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }
}
